package controller;

import com.google.gson.Gson;
import entry.Enterprise;
import entry.Instituty;
import entry.Ticket_open;
import pojo.XianShi;
import service.EnterpriseService;
import service.InstitutyService;
import service.Review_recordService;
import service.impl.EnterpriseServiceImpl;
import service.impl.InstitutyServiceImpl;
import service.impl.Review_recordServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
@SuppressWarnings("all")
@WebServlet("/Review_recordServlet/*")
public class Review_recordServlet extends HttpServlet {
    private EnterpriseService enterpriseService = new EnterpriseServiceImpl();
    private InstitutyService institutyService = new InstitutyServiceImpl();
    private Review_recordService review_recordService = new Review_recordServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //0、处理乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

        //1.获得请求路径
        String url = request.getRequestURI();
        String methodName = url.substring(url.lastIndexOf("/")+1);
        Class cla = Review_recordServlet.class;
        try {
            Method method = cla.getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(cla.newInstance(),request,response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void queryByStatues(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ArrayList<Ticket_open> messages = review_recordService.queryByStatues();
        //System.out.println(messages+"////////////////////////////");
        ArrayList<XianShi> details = new ArrayList<XianShi>();

        for (int i = 0; i < messages.size(); i++) {
            XianShi detail = new XianShi();
            Instituty instName = institutyService.selectNameById(messages.get(i).getInstituty_id());
            Enterprise topen = enterpriseService.selectNameById(messages.get(i).getEnterprise_id());
            Enterprise acquTopen1 = enterpriseService.selectNameById(messages.get(i).getAcquire_enterprise_id());
            // System.out.println(acquTopen1+"//////////////////////");
            detail.setNo(messages.get(i).getNo());
            detail.setAmount(messages.get(i).getAmount());
            detail.setKaidanTime(messages.get(i).getCreate_time());
            detail.setStatus(messages.get(i).getStatus());
            detail.setShangLian(messages.get(i).getUplink_address());
            detail.setShouDanTime(messages.get(i).getExpiry_time());
            detail.setInstituName(instName.getName());
            detail.setKaiDanName("百度科技");
            detail.setShouDanName(acquTopen1.getName());
            details.add(detail);
        }
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(details));
    }

    public void showMessage(HttpServletResponse response, ArrayList<Ticket_open> ticket_opens) throws IOException {
        ArrayList<XianShi> messages = new ArrayList<>();
        for (Ticket_open ticket_open : ticket_opens) {
            XianShi detail = new XianShi();
            Instituty instName = institutyService.selectNameById(ticket_open.getInstituty_id());
            Enterprise acquTopen1 = enterpriseService.selectNameById(ticket_open.getAcquire_enterprise_id());
            detail.setNo(ticket_open.getNo());
            detail.setAmount(ticket_open.getAmount());
            detail.setKaidanTime(ticket_open.getCreate_time());
            detail.setStatus(ticket_open.getStatus());
            detail.setShangLian(ticket_open.getUplink_address());
            detail.setShouDanTime(ticket_open.getExpiry_time());
            detail.setInstituName(instName.getName());
            detail.setKaiDanName("百度科技");
            detail.setShouDanName(acquTopen1.getName());
            messages.add(detail);
        }
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(messages));
    }
}
